<!--  我的车辆列表/车辆管理列表
 // 权限分类：根据webUserInfo判断是否平台用户，区别普通大众和平台账户
 // 园区管理员：查看所有车辆数据（修改车辆：不进行车辆绑定，只修改车辆信息）
 // 普通大众：只能查看自己绑定的车辆数据（修改车辆：不再进行车辆绑定，只修改车辆信息）
 // 园区工作人员、企业管理员：没有模块权限
-->
<template>
	<view class="main-contanier">
		<view class="head-search">
			<uni-search-bar class="search-bar" ref="search" placeholder="搜索" bgColor="#EEEFF1" :radius="100"
				@confirm="search" @blur="search" @input="input" cancelButton="none">
			</uni-search-bar>
			<view class="btn-content" v-if="!webUserInfo">
				<image src="@/static/img/icon/icon_add.png" class="img" mode="aspectFill" @click="addCarInfo">
				</image>
			</view>
		</view>
		<view class="main-box">
			<CarItem :list="listData" @clickItem="goDetail"></CarItem>
			<uni-load-more :status="loadingType" :icon-size="iconsize" :content-text="contentText"
				@clickLoadMore="clickLoadMore" />
		</view>
	</view>
</template>

<script>
	import {
		Platform,
		Storage_Key,
		ProjectStates,
		ProjectKeys
	} from '@/static/config.js';
	import request from '@/common/request.js';
	import CarItem from "./components/car-item/car-item.vue"

	let that;
	export default {
		components: {
			CarItem
		},
		data() {
			return {
				// 数据列表
				listData: [],
				// 查询条件
				pageIndex: 1,
				pageSize: 10,
				doquerytime: Date.now(), // 每次执行初始查询时 将其置为当时时间，后续分页以该时间为基准进行查询。
				keyword: "",
				//加载更多
				refreshing: false,
				loadingType: 'more',
				iconsize: 16,
				contentText: {
					contentdown: '加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				wxUserInfo: [], //微信用户
				webUserInfo: [], //web平台用户
			}
		},
		beforeCreate() {
			that = this;
		},
		onLoad(options) {
			if (options && options.data) {}
			this.setListening(); // 设置监听
		},
		mounted() {},
		onShow() {
			this.clear();
		},
		/**
		 *  点击导航栏 buttons 时触发
		 */
		onNavigationBarButtonTap() {
			// let title = this.dataInfo.pumpName;
			// this.toRunwork(this.dataInfo);
		},
		//加载更多
		onReachBottom() {
			this.pageIndex++;
			this.loadData();
		},
		onPullDownRefresh() { // 下拉刷新
			this.refreshing = true;
			this.pageIndex = 1;
			this.doquerytime = Date.now();
			this.loadingType = "more";
			this.listData = [];
			this.loadData().then(rs => {
				uni.stopPullDownRefresh();
				this.refreshing = false;
			});
		},
		computed: {},
		methods: {
			// 设置监听
			setListening() {
				let _self = this;
				uni.$on('my-car-list-refresh', function(data) {
					if (data.key) {
						_self.clear();
					} else {}
				})
			},
			clear(e) {
				this.wxUserInfo = this.$storage.get(Storage_Key.user_info, true);
				this.webUserInfo = this.$storage.get(Storage_Key.web_user_info, true);
				this.pageIndex = 1;
				this.doquerytime = Date.now();
				this.loadingType = "more";
				this.listData = [];
				this.keyword = "";
				//判断是否为游客，游客不能进行查询数据
				if (!!this.wxUserInfo || !!this.webUserInfo) {
					this.loadData();
				}
			},
			async loadData() {
				let _self = this;
				// let creator = this.$storage.get(Storage_Key.user_info, true).openId || "";
				if (_self.loadingType == "nomore") return;
				let _list = [];
				let totalcount = -1;
				let formData = {
					pageIndex: _self.pageIndex,
					pageSize: _self.pageSize,
					carCode: _self.keyword,
					// openid:this.wxUserInfo.openId
				};
				// 权限分类：根据webUserInfo判断是否平台用户，区别普通大众和平台账户
				// 园区管理员：查看所有车辆数据（修改车辆：不进行车辆绑定，只修改车辆信息）
				// 普通大众：只能查看自己绑定的车辆数据（修改车辆：不再进行车辆绑定，只修改车辆信息）
				// 园区工作人员、企业管理员：没有模块权限
				if (!_self.webUserInfo) { // 普通大众
					formData.openid = _self.wxUserInfo.openId;
				}
				// 24.12.16加   所有人都需要openid
				formData.openid = _self.wxUserInfo.openId;
				// uni.showLoading({
				// 	title: '加载中...'
				// });
				request({
					url: Platform.api_requestUrl + '/stp-car/getListDetail',
					method: 'GET',
					data: formData,
				}).then(res => {
					uni.hideLoading();
					if (res.code == 200 && !!res.data) {
						_list = res.data;
						_self.listData = [..._self.listData, ..._list];
						totalcount = res.total;
					}
					if (_self.listData.length >= totalcount)
						_self.loadingType = "nomore"
				}).catch(error => {
					uni.hideLoading();
					console.log("fail")
				});
				// uni.request({
				// 	url: Platform.api_requestUrl + '/stp-car/getListDetail',
				// 	data: formData,
				// 	header: {},
				// 	method: 'GET',
				// 	success: (res) => {
				// 		//uni.hideLoading();
				// 		if (res.data.code == 200 && !!res.data.data) {
				// 			_list = res.data.data;
				// 			_self.listData = [..._self.listData, ..._list];
				// 			totalcount = res.data.total;
				// 		}
				// 		console.log(_self.listData)
				// 		if (_self.listData.length >= totalcount)
				// 			_self.loadingType = "nomore"
				// 	},
				// 	complete: () => {
				// 		console.log("complete")
				// 	},
				// 	fail: (error) => {
				// 		uni.hideLoading();
				// 		console.log("fail")
				// 	},
				// });
			},
			// 关键字搜索
			search(e) {
				this.pageIndex = 1;
				this.doquerytime = Date.now();
				this.loadingType = "more";
				this.listData = [];
				this.keyword = e ? e.value : "";
				this.loadData();
			},
			input(e) {
				// 只处理清空
				if (!e.value) {
					this.search();
				}
			},
			goDetail(item) {
				uni.navigateTo({
					url: '/pages/car/my-car-add?item=' + encodeURIComponent(JSON.stringify(item)) +
						'&&disabled=true'
				})
			},
			// 点击加载更多
			clickLoadMore(e) {
				this.pageIndex++;
				this.loadData();
			},
			//添加车辆
			addCarInfo() {
				uni.redirectTo({
					url: '/pages/car/my-car-add'
				})
			},
		},
		filters: {

		}
	}
</script>
<style lang='scss'>
	.main-contanier {
		position: relative;
	}

	.main-box {
		/* flex: 1; */
		overflow: scroll;
		background: #FFFFFF;
		/* padding: 15px; */
		padding-top: 55px;
		min-height: 86vh;
	}
</style>
